package com.open.party.cloud.view.home.jiCengDangJian

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.PartyDevBean
import com.open.party.cloud.view.home.jiCengDangJian.adapter.DyfzDevStepListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DyfzMainActivity : AppRecycleViewTitleBaseActivity<PartyDevBean>(), OnSuperListener<PartyDevBean> {

    private var viewModel: BizViewModel? = null
    private var adapter: DyfzDevStepListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("党员发展")

        viewModel = BizViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = DyfzDevStepListAdapter(this)
        adapter!!.setOnSuperListener(this)
        loadingRecyclerView.adapter = adapter

        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }


    override fun loadData(pageVo: PageVo<PartyDevBean>) {
        viewModel!!.getDangYuanFaZhanListData()
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<HashMap<String, ArrayList<PartyDevBean>>>) {

        if ("getDangYuanFaZhanListData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    showData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun showData(data: HashMap<String, ArrayList<PartyDevBean>>) {
        val dataList = ArrayList<PartyDevBean>()

        if (data["申请入党"] != null) {
            dataList.addAll(data["申请入党"]!!)
        }
        if (data["入党积极分子的确定和培养教育"] != null) {
            dataList.addAll(data["入党积极分子的确定和培养教育"]!!)
        }
        if (data["发展对象的确定和考察"] != null) {
            dataList.addAll(data["发展对象的确定和考察"]!!)
        }
        if (data["预备党员的接收"] != null) {
            dataList.addAll(data["预备党员的接收"]!!)
        }
        if (data["预备党员的教育考察和转正"] != null) {
            dataList.addAll(data["预备党员的教育考察和转正"]!!)
        }

        if (dataList.size == 0) {
            loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
            loadingRecyclerView.setLoadingMoreEnabled(false)
        } else {
            adapter!!.setData(dataList)
            loadingTipView.showContent()
        }
    }

    override fun onClick(position: Int, itemData: PartyDevBean, flag: String?) {
        XUtils.intent().openActivity(this@DyfzMainActivity, DyfzFileListActivity::class.java)
            .putStringExtra("code", itemData.progress.code)
            .start()
    }
}